package com.shuwen.gcdj.controller.bind;

import com.shuwen.gcdj.bean.rolepower.response.PowerLevelResp;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.bean.VoidResp;
import com.shuwen.gcdj.bean.role.request.MultiBindRolePowerReq;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.common.enums.LogModuleEnum;
import com.shuwen.gcdj.common.enums.LogTypeEnum;
import com.shuwen.gcdj.common.sysLog.AddSysLogUtil;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.dto.account.InitUserDto;
import com.shuwen.gcdj.service.account.AccountRoleService;
import com.shuwen.gcdj.service.account.AccountService;
import com.shuwen.gcdj.service.role.RolePowerService;
import com.shuwen.gcdj.service.role.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户中心 - 角色权限码 (可用)")
@RestController
@RequestMapping("/rolePower")
@Slf4j
public class RolePowerController extends AuthController {

    @Autowired
    private RolePowerService rolePowerService;

    @Autowired
    private AddSysLogUtil addSysLogUtil;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private AccountRoleService accountRoleService;

    @ApiOperation(value = "绑定角色权限码(可用)")
    @PostMapping(value = "/bind")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> bindRolePower(HttpServletRequest request, @Valid @RequestBody MultiBindRolePowerReq req) {
        InitUserDto initUserDto = initUser(request);
        rolePowerService.bindRolePowerMulti(req, initUserDto);
        addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_ADD.getTypeNum(), LogModuleEnum.LOG_MODULE_ROLE.getModuleNum(), "给角色" + req.getRoleId() + "添加了权限" + req.getPowers(), req);
        return CommonResult.success(new VoidResp());
    }


    @ApiOperation(value = "根据角色获取权限码配置(可用)")
    @GetMapping(value = "/getPowers/{roleId}")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<PowerLevelResp>> getPowers(HttpServletRequest request, @Valid @PathVariable String roleId) {
        return CommonResult.success(rolePowerService.getPowersByRoleId(roleId));
    }

    @ApiOperation(value = "获取所有菜单及菜单下面的权限")
    @GetMapping(value = "/getAllRolePowers")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<PowerLevelResp>> getAllRolePowers(HttpServletRequest request) {
        return CommonResult.success(rolePowerService.findAllList());
    }
}
